package HouseView;

import HouseService.service;
import Utility.Utility;
import domain.House;

public class view {
    private boolean loop = true;//循环条件
    private char key = ' ';//键盘输入
    private service Service = new service(10);

    public void update() {
        System.out.println("==============修改房屋信息==============");
        System.out.println("请选择删除房屋编号（-1退出）：");
        int updateid = Utility.readInt();
        if (updateid == -1) {
            System.out.println("==============放弃修改房屋信息==============");
            return;
        }
        House house = service.findbyid(updateid);
        if (house == null) {
            System.out.println("修改房屋信息不存在");
        }
        System.out.print("姓名（" + house.getName() + "）:");
        String name = Utility.readString(8, "");
        if (!"".equals(name)) {
            house.setName(name);
        }
        System.out.print("电话（" + house.getPhone() + "）:");
        String phone = Utility.readString(12, "");
        if (!"".equals(phone)) {
            house.setPhone(phone);
        }
        System.out.print("地址（" + house.getAddress() + "）:");
        String address = Utility.readString(16, "");
        if (!"".equals(address)) {
            house.setAddress(address);
        }
        System.out.print("租金（" + house.getName() + "）:");
        int rent = Utility.readInt(-1);
        if (!"".equals(-1)) {
            house.setRent(rent);
        }
        System.out.print("状态（" + house.getState() + "）:");
        String state = Utility.readString(3, "");
        if (!"".equals(state)) {
            house.setState(state);
        }
        System.out.println("==============修改房屋信息成功==============");

    }


    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            System.out.println("退出房屋系统");
            loop = false;
        } else {
            System.out.println("取消退出成功");
        }
    }

    public void findhouse() {

        System.out.print("输入寻找编号：");
        int findid = Utility.readInt();
        System.out.println("==============寻找房屋信息==============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态（未出租/已出租）");
        System.out.println(service.findbyid(findid));

    }

    public void delhouse() {
        System.out.println("==============删除房屋信息==============");
        System.out.print("请选择删除房屋编号（-1退出）：");
        int delid = Utility.readInt();
        if (delid == -1) {
            System.out.println("==============放弃删除房屋信息==============");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (service.del(delid)) {
                System.out.println("==============成功删除房屋信息==============");
            } else {
                System.out.println("==============删除房屋信息不存在==============");
            }
        } else {
            System.out.println("==============放弃删除房屋信息==============");
        }
    }

    public void listHouse() {
        System.out.println("==============房屋列表==============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态（未出租/已出租）");
        House[] houses = service.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("==============房屋列表显示完毕==============");
    }

    public void addhouse() {
        System.out.println("==============添加房屋==============");
        System.out.print("姓名：");
        String name = Utility.readString(8);
        System.out.print("电话：");
        String phone = Utility.readString(12);
        System.out.print("地址：");
        String address = Utility.readString(16);
        System.out.print("月租：");
        int rent = Utility.readInt();
        System.out.print("状态（未出租/已出租）：");
        String state = Utility.readString(3);
        final House newhouse = new House(0, name, phone, address, rent, state);
        if (service.add(newhouse)) {
            System.out.println("房屋添加成功");
        } else {
            System.out.println("房屋添加失败");
        }


    }

    public void houseMenu() {
        do {
            System.out.println("==============房屋出租系统==============");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 源");
            System.out.println("\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退      出");
            System.out.print("请选择（1~6）:");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addhouse();
                    break;
                case '2':
                    findhouse();
                    break;
                case '3':
                    delhouse();
                    break;
                case '4':
                    update();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("你的输入有误，请重新尝试");

            }
        } while (loop);
    }
}
